/**
 * 
 */
package sudoku.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import sudoku.controller.Controller;
import sudoku.util.Config;
import sudoku.util.Language;

/**
 * @author Romet
 * 
 */
public class UsersUI implements ActionListener, ListSelectionListener {

  private volatile SudokuUI m2ng;
  private volatile Controller controller;
  private volatile Config config;
  private volatile Language language;

  private JList valik;
  private JButton[] options = new JButton[4];
  private JDialog dialog;

  public UsersUI(Language language, SudokuUI m2ng, Controller controller, Config config) {
    this.language = language;
    this.m2ng = m2ng;
    this.controller = controller;
    this.config = config;

    DefaultListModel listModel = new DefaultListModel();
    for (int i = 0; i < this.config.getKasutajad().size(); i++) {
      listModel.addElement((this.config.getKasutajad().get(i)).split(":")[0]);
    }
    valik = new JList(listModel);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(options[0])) {
      config.setHetke_kasutaja(valik.getSelectedIndex());
      config.setKasutajad(config.getKasutajad());
      controller.handleEvent(Controller.VALITUD_KASUTAJA_EVENT);
      dialog.dispose();
    }
    else if (e.getSource().equals(options[1])) {
      dialog.dispose();
      config.setHetke_kasutaja(-1);
      controller.handleEvent(Controller.MUUDA_KASUTAJA_EVENT);
    }
    else if (e.getSource().equals(options[2])) {
      config.setHetke_kasutaja(valik.getSelectedIndex());
      dialog.dispose();
      controller.handleEvent(Controller.MUUDA_KASUTAJA_EVENT);
    }
    else if (e.getSource().equals(options[3])) {
      List<String> list = config.getKasutajad();
      list.remove(valik.getSelectedIndex());
      if (config.getHetke_kasutaja() == valik.getSelectedIndex()) {
        config.setHetke_kasutaja(-1);
      }
      else {
        if (config.getHetke_kasutaja() > valik.getSelectedIndex()) {
          config.setHetke_kasutaja(config.getHetke_kasutaja() - 1);
        }
      }
      config.setKasutajad(list);
      config.salvesta();
      m2ng.uuendaUI();
      dialog.dispose();
      controller.handleEvent(Controller.VALI_KASUTAJA_EVENT);
    }
  }

  public void valiKasutaja() {
    options[0] = new JButton(this.language.getUsers_select());
    options[0].setEnabled(false);
    options[0].addActionListener(this);
    options[1] = new JButton(this.language.getUsers_new());
    options[1].addActionListener(this);
    options[2] = new JButton(this.language.getUsers_change());
    options[2].setEnabled(false);
    options[2].addActionListener(this);
    options[3] = new JButton(this.language.getUsers_delete());
    options[3].setEnabled(false);
    options[3].addActionListener(this);
    valik.addListSelectionListener(this);
    if (config.getKasutajad().size() > 15) {
      valik.setVisibleRowCount(15);
    }
    else {
      valik.setVisibleRowCount(config.getKasutajad().size());
    }
    JScrollPane scrollPane = new JScrollPane(valik);
    JOptionPane pane = new JOptionPane(scrollPane);
    pane.setOptions(options);
    dialog = pane.createDialog(m2ng, this.language.getUsers_title());
    dialog.setVisible(true);
  }

  @Override
  public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting() == false) {
      if (valik.getSelectedIndex() == -1) {
        options[0].setEnabled(false);
        options[2].setEnabled(false);
        options[3].setEnabled(false);
      }
      else {
        options[0].setEnabled(true);
        options[2].setEnabled(true);
        options[3].setEnabled(true);
      }
    }
  }
}
